package com.tsunami.security.uaa.service.security;

import com.alibaba.fastjson.JSONObject;
import com.tsunami.authority.entity.auth.User;
import com.tsunami.authority.enums.auth.Sex;
import com.tsunami.base.R;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.security.uaa.model.SecurityUser;
import com.tsunami.security.uaa.provider.AdminProvider;
import com.tsunami.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname UserServiceImpl
 * @Description TODO
 * @Date 2021-11-10 10:40
 * @Created by lx
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private AdminProvider adminProvider;

    @Override
    public UserDetails loadUserByUsername(String s) {
        R userStr = adminProvider.selectByAccount(s);
        Map userMap = userStr.getExtra();

        if(userMap ==null){
            throw  new BizException(ExceptionCode.NO_USER);
        }
        User user = getUserByObject(userMap.get("data"));
        R permissionResult = adminProvider.findVisibleResource(user.getId());
        List<String> permissionValueList = new ArrayList<>();
        if(null!=permissionResult&&permissionResult.getCode()==200){
            Map permissionMap = permissionResult.getExtra();
            permissionValueList = (List<String>) permissionMap.get("data");
        }
        SecurityUser loginUser = new SecurityUser();
        loginUser.setUser(user);
        loginUser.setPermissions(permissionValueList);

        return loginUser;
    }

    User getUserByObject(Object o){
        JSONObject userObject = (JSONObject) JSONObject.toJSON(o);
        User user = new User();
        user.setId(userObject.getLong("id"));
        user.setAccount(userObject.getString("account"));
        user.setAvatar(userObject.getString("avatar"));
        user.setEmail(userObject.getString("email"));
        user.setMobile(userObject.getString("mobile"));
        user.setName(userObject.getString("name"));
        user.setOrgId(userObject.getLong("orgId"));
        user.setPassword(userObject.getString("password"));
        user.setPasswordErrorNum(userObject.getInteger("passwordErrorNum"));
        JSONObject sex = userObject.getJSONObject("sex");
        if(sex.get("code").equals(Sex.M.getCode())){
            user.setSex(Sex.M);
        }else if(sex.get("code").equals(Sex.W.getCode())){
            user.setSex(Sex.W);
        }else {
            user.setSex(Sex.N);
        }
        user.setPasswordExpireTime(TimeUtil.StringToJava8Time((String) userObject.get("passwordExpireTime")));
        user.setAccountExpireTime(TimeUtil.StringToJava8Time((String) userObject.get("accountExpireTime")));
        user.setStationId(userObject.getLong("stationId"));
        user.setStatus(userObject.getString("status"));
        user.setTenantId(userObject.getLong("tenantId"));
        user.setWorkDescribe(userObject.getString("workDescribe"));
        user.setCreateUser(userObject.getLong("createUser"));
        user.setPasswordErrorLastTime(TimeUtil.StringToJava8Time((String) userObject.get("passwordErrorLastTime")));
        return user;
    }
}
